package com.icecloud.cloud.filestore.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.icecloud.cloud.filestore.common.RestfulResponseBody;
import com.icecloud.cloud.filestore.service.FileStoreService;
import com.mongodb.gridfs.GridFSDBFile;

/**
 * 测试核心用户
 * 
 * @author penghaozhong
 *
 */
@RestController
public class FileStoreController {
	
	
	@Autowired
	private FileStoreService fileStoreService;
	
	

	/**
	 * 保存文件
	 * @param file
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/file",method=RequestMethod.POST)
	public ResponseEntity<RestfulResponseBody> save(@RequestParam(value = "file", required = true) MultipartFile file,String access_token) throws IOException {
		RestfulResponseBody body = new RestfulResponseBody();
		String id = fileStoreService.saveFile(access_token,file.getOriginalFilename(), file.getContentType(), file);
		if(StringUtils.isNotBlank(id)){
			body.setReason("上传成功");
			body.setResultFlag(1);
			body.put("id", id);
		}else{
			body.setReason("上传是不");
			body.setResultFlag(-1);
		}
		return new ResponseEntity<RestfulResponseBody>(body,HttpStatus.OK);
	}
	
	/**
	 * 获取文件
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/file/{id}")
	public ResponseEntity<byte[]> get(@PathVariable String id) throws Exception {
		HttpHeaders headers = new HttpHeaders();
		GridFSDBFile file = fileStoreService.getFileById(id);
		if(file != null){
			headers.setContentType(MediaType.parseMediaType(file.getContentType()));
			return new ResponseEntity<byte[]>(IOUtils.toByteArray(file.getInputStream()), headers, HttpStatus.OK);
		}else{
			throw new Exception("文件不存在");
		}
	}
	
	/**
	 * 获取文件列表
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/file")
	public ResponseEntity<RestfulResponseBody> findlist(String access_token) throws Exception {
		List<GridFSDBFile> file = fileStoreService.getFileList(access_token);
		RestfulResponseBody body = new RestfulResponseBody();
		List<String> list =new ArrayList<String>();
		if(CollectionUtils.isNotEmpty(file)){
			for (GridFSDBFile gridFSDBFile : file) {
				list.add(gridFSDBFile.toString());
			}
		}
		body.put("list",list);
		return new ResponseEntity<RestfulResponseBody>(body,HttpStatus.OK);
	}
	
}
